package tree;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class SolutionOne {
        public static List<Integer> rightSideView(TreeNode root) {
            List<Integer> ret = new ArrayList<>();
            if(root == null){
                return ret;
            }
            Deque<TreeNode> queue = new LinkedList<>();

            queue.offer(root);
            while(!queue.isEmpty()){
                int size = queue.size();
                while(size > 0){
                    root = queue.pop();
                    if(size == 1){
                        ret.add(root.val);
                    }
                    if(root.left != null){
                        queue.offer(root.left);
                    }
                    if(root.right != null){
                        queue.offer(root.right);
                    }
                    size--;
                }

            }
            return ret;
        }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        init(root);
        rightSideView(root);
    }

    private static void init(TreeNode root){
        root.left = new TreeNode(2);
        root.right = new TreeNode(3);
        root.left.right = new TreeNode(5);
        root.right.right = new TreeNode(4);

    }

    public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
}
